package com.demo.cms.action.manage;

import java.util.ArrayList;
import java.util.List;

import com.demo.cms.model.Article;
import com.demo.cms.model.ArticleComment;
import com.demo.cms.service.ArticleCommentService;
import com.demo.cms.service.ArticleService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Site;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;

@Function(id = 104300, pid = 104200, code = "manage.article.comment")
public class ArticleCommentAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 3987575580155587826L;

	/**
	 * 评论主键ID
	 */
	private Long id;
	
	/**
	 * 文章主键ID
	 */
	private Long articleId;
	
	/**
	 * 评论类型
	 */
	private String type;
	
	/**
	 * 评论展示模式 0:列表展示;1:树形展示等
	 */
	private int mode = 0;
	
	/**
	 * 文章模型
	 */
	private Article article;
	
	/**
	 * 评论模型
	 */
	private ArticleComment articleComment;
	
	/**
	 * 评论列表
	 */
	private List<ArticleComment> articleCommentList;
	
	/**
	 * 文章服务
	 */
	private ArticleService articleService;

	/**
	 * 评论服务
	 */
	private ArticleCommentService articleCommentService;
	
	/**
	 * 评论列表
	 * @return String
	 */
	@Function(id = 104310, pid = 104300, code = "manage.article.comment.list")
	public String list() {
		
		Site site = getSite();
		Long siteId = site.getId();
		
		SearchArgumentSupport args = getArgumentSupport();
		
		ResultSearchResult<ArticleComment>  resultSupport = null;
		
		if (mode < 1) {
			resultSupport = articleCommentService.search(siteId, type, args);
		} else {
			// 树形结构
			resultSupport = articleCommentService.search(siteId, type, args);
		}
		this.setResultSupport(resultSupport);
		this.articleCommentList = new ArrayList<ArticleComment>(resultSupport.getResults());
		
		return SUCCESS;
	}
	
	/**
	 * 创建评论
	 * @return String
	 */
	@Function(id = 104320, pid = 104300, code = "manage.article.comment.create")
	public String create() {
		//articleCommentService.save(this.articleComment);
		return SUCCESS;
	}
	
	/**
	 * 创建评论
	 * @return String
	 */
	@Function(id = 104330, pid = 104300, code = "manage.article.comment.save")
	public String save() {
		articleCommentService.save(this.articleComment);
		addActionMessage("评论保存成功");
		return CALLBACK;
	}
	
	/**
	 * 评论修改
	 * @return String
	 */
	@Function(id = 104340, pid = 104300, code = "manage.article.comment.edit")
	public String edit() {
		
		this.articleComment = articleCommentService.find(id);
		if (articleComment != null && articleComment.getArticleId() != null) {
			this.article = articleService.simpleFind(articleComment.getArticleId());
		}
		if (this.article == null) {
			addActionError("该文章不存在.");
		}
		return SUCCESS;
	}
	
	/**
	 * 评论状态更新(审核)
	 * @return String
	 */
	@Function(id = 104350, pid = 104300, code = "manage.article.comment.update")
	public String update() {
		
		addActionMessage("评论更新成功");
		articleCommentService.update(id, articleComment);
		
		return CALLBACK;
	}
	
	/**
	 * 删除评论
	 * @return String
	 */
	@Function(id = 104360, pid = 104300, code = "manage.article.comment.delete")
	public String delete() {
		
		articleCommentService.delete(id);
		addActionMessage("评论删除成功");
		return CALLBACK;
	}
	
	// ~ settings here ========================================================================
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public Long getArticleId() {
		return articleId;
	}

	public void setArticleId(Long articleId) {
		this.articleId = articleId;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleComment getArticleComment() {
		return articleComment;
	}

	public List<ArticleComment> getArticleCommentList() {
		return articleCommentList;
	}

	public void setArticleComment(ArticleComment articleComment) {
		this.articleComment = articleComment;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public void setArticleCommentService(ArticleCommentService articleCommentService) {
		this.articleCommentService = articleCommentService;
	}
}
